<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" width="800px" append-to-body @close="closeDialog">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row v-if="form.id">
        <el-col :span="12">
          <el-form-item label="合同编号" prop="contractNo">
            <el-input v-model="form.contractNo" placeholder="请输入合同编号" :disabled="true" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="人员ID" prop="tenantId">
            <el-input v-model="form.tenantId" placeholder="请输入人员ID" :disabled="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同类型" prop="contractType">
            <el-radio-group v-model="form.contractType">
              <el-radio
                v-for="dict in dict.type.contract_type"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同状态" prop="contractStatus">
            <el-select v-model="form.contractStatus" placeholder="请选择合同状态">
              <el-option
                v-for="dict in dict.type.contract_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="签约日期" prop="signDate">
            <el-date-picker clearable
              v-model="form.signDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择签约日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同开始日期" prop="startDate">
            <el-date-picker clearable
              v-model="form.startDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择合同开始日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="合同结束日期" prop="endDate">
            <el-date-picker clearable
              v-model="form.endDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择合同结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="月租金(元)" prop="rentAmount">
            <el-input v-model="form.rentAmount" placeholder="请输入月租金(元)" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="押金(元)" prop="depositAmount">
            <el-input v-model="form.depositAmount" placeholder="请输入押金(元)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="每月缴费日" prop="paymentDay">
            <el-input v-model="form.paymentDay" placeholder="请输入每月缴费日" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="支付方式" prop="paymentMethod">
            <el-select v-model="form.paymentMethod" placeholder="请选择支付方式">
              <el-option label="月付" value="1"></el-option>
              <el-option label="季付" value="2"></el-option>
              <el-option label="半年付" value="3"></el-option>
              <el-option label="年付" value="4"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="合同附件" prop="attachmentUrls">
            <file-upload v-model="form.attachmentUrls"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="closeDialog">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addContract, updateContract } from "@/api/lock/contract";

export default {
  name: "ContractDialog",
  dicts: ['contract_type', 'contract_status'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ""
    },
    formData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        id: null,
        contractNo: null,
        tenantId: null,
        contractType: null,
        contractStatus: null,
        signDate: null,
        startDate: null,
        endDate: null,
        rentAmount: null,
        depositAmount: null,
        paymentMethod: null,
        paymentDay: null,
        remark: null,
        attachmentUrls: null
      },
      rules: {
        contractType: [
          { required: true, message: "合同类型不能为空", trigger: "change" }
        ],
        contractStatus: [
          { required: true, message: "合同状态不能为空", trigger: "change" }
        ],
        signDate: [
          { required: true, message: "签约日期不能为空", trigger: "blur" }
        ],
        startDate: [
          { required: true, message: "合同开始日期不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "合同结束日期不能为空", trigger: "blur" }
        ],
        rentAmount: [
          { required: true, message: "月租金(元)不能为空", trigger: "blur" }
        ],
        depositAmount: [
          { required: true, message: "押金(元)不能为空", trigger: "blur" }
        ],
        paymentMethod: [
          { required: true, message: "支付方式不能为空", trigger: "change" }
        ],
        paymentDay: [
          { required: true, message: "每月缴费日不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    visible(val) {
      this.dialogVisible = val;
    },
    formData: {
      handler(val) {
        if (val) {
          this.form = { ...val };
        }
      },
      immediate: true
    }
  },
  methods: {
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.closeDialog();
              this.$emit("refresh");
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.closeDialog();
              this.$emit("refresh");
            });
          }
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        contractNo: null,
        tenantId: null,
        contractType: null,
        contractStatus: null,
        signDate: null,
        startDate: null,
        endDate: null,
        rentAmount: null,
        depositAmount: null,
        paymentMethod: null,
        paymentDay: null,
        remark: null,
        attachmentUrls: null
      };
      this.$refs["form"] && this.$refs["form"].resetFields();
    }
  }
};
</script> 